package com.lzp.goods.controller;

import com.github.pagehelper.Page;
import com.lzp.entity.PageResult;
import com.lzp.entity.Result;
import com.lzp.entity.StatusCode;
import com.lzp.goods.pojo.Brand;
import com.lzp.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
//        int i = 1/0;
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        Brand brand = brandService.findById(id);
        if (brand != null) {
            return new Result(true, StatusCode.OK, "查询成功", brand);
        } else {
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 添加品牌
     *
     * @RequestBod表示如果你使用的post或者put请求的话，我们从请 求体中获取json数据
     * 然后赋予给Brand实体参数
     */
    @PostMapping("/add")
    public Result add(@RequestBody Brand brand) {
        try {
            brandService.add(brand);
            return new Result(true, StatusCode.OK, "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "新增失败");
        }
    }

    /**
     * 修改品牌
     *
     * @param brand
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Brand brand) {
        try {
            brandService.update(brand);
            return new Result(true, StatusCode.OK, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    /**
     * 根据id删除品牌
     *
     * @param id
     * @return
     */

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            brandService.deleteById(id);
            return new Result(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "删除失败");
        }
    }

    /**
     * 根据条件查询
     * 请求参数是实体不用加@RequestParam注解,如果是map的话需要加
     * 另外get请求方式没有请求体  也不用加@Requestbody注解
     *
     * @param brand
     * @return
     */
    @GetMapping("/search")
    public Result search(Brand brand) {
        List<Brand> brandList = brandService.search(brand);
        if (brandList.size() <= 0 || brandList == null) {
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * 单独分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/page/{page}/{size}")
    public PageResult findByPage(@PathVariable int page, @PathVariable int size) {
        try {
            Page<Brand> brandPage = brandService.findByPage(page, size);
            long total = brandPage.getTotal();
            List<Brand> rows = brandPage.getResult();
            return new PageResult(total, rows);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(null, null);
        }
    }


    @GetMapping("/page2/{page}/{size}")
    public PageResult findByPage2(@PathVariable int page, @PathVariable int size, Brand brand) {
        try {
            Page<Brand> brandPage = brandService.findByPage2(page, size, brand);
            long total = brandPage.getTotal();
            List<Brand> rows = brandPage.getResult();
            return new PageResult(total, rows);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(null, null);
        }
    }
}
